// 想要封装的工具生效，代码需要被执行
//把utils引入到app.js
import './utils/utils'
//把http引入到app.js
import './utils/http'

// app.js
App({
  globalData: {},
  token: '',
  refresh_token: '',
  onLaunch() {
    // 小程序一启动就调用获取token方法
    this.getToken('token')
    this.getToken('refresh_token')
  },
  // 传入token/refresh_token
  getToken(key) {
    wx.getStorage({
      key,
      success: (res) => {
        console.log(`${key}为：` + res.data)
        this[key] = res.data
      },
    })
  },
  setToken(token, refresh_token) {
    // 拼凑token
    token = 'Bearer ' + token
    refresh_token = 'Bearer ' + refresh_token
    // 存入token
    wx.setStorageSync('token', token)
    wx.setStorageSync('refresh_token', refresh_token)
    // 全局中的token也需要更新
    this.token = token
    this.refresh_token = refresh_token
  },
})
